package Protocol;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Command {
	public static String generate(Package pkg) {
		return "==host==:" + pkg.getHost() + "==action==:" + pkg.getAction()
				+ "==detail==:" + pkg.getDetail();
	}

	public static Package parse(String str) {
		String regex = "^==host==:([\\s\\S]*?)==action==:([\\s\\S]*?)==detail==:([\\s\\S]*?)$";
		Matcher m = Command.matchRegex(regex, str);
		if (m.find()) {
			return new Package(m.group(1), m.group(2), m.group(3));
		}
		return null;
	}

	private static Matcher matchRegex(String regex, String input) {
		return Pattern.compile(regex).matcher(input);
	}
}
